import 'dart:async';

import 'package:bruno/bruno.dart';
import 'package:flutter/material.dart';
import 'package:flutter/scheduler.dart';
import 'package:hygf/http/file_transfer/down_cap.dart';
import 'package:hygf/page/home/data/view.dart';
import '../../http/DioClient.dart';
import 'package:hygf/page/MinePage.dart';
import 'package:hygf/page/home/application/view.dart';
import '../config/GlobalConfig.dart';
import '../res/HDColors.dart';
import '../util/location/location_gaode.dart';

class MainPage extends StatefulWidget {
  const MainPage({super.key});

  @override
  _MainPage createState() => _MainPage();
}

class _MainPage extends State<MainPage> with WidgetsBindingObserver {
  double iconWidth = 30;
  double iconHeight = 30;
  int _currentIndex = 0;
  List<BottomNavigationBarItem> bottomItems = [];
  Widget _currBody = ApplicationPage();
  DateTime? _lastPressedAt; //上次点击时间 应用
  bool showVersionDot = false;
  StreamSubscription? subscription;

  @override
  void didChangeAppLifecycleState(AppLifecycleState state) {
    super.didChangeAppLifecycleState(state);
    if (state == AppLifecycleState.resumed) {}
  }

  @override
  void initState() {
    super.initState();
    WidgetsBinding.instance.addObserver(this);
    if (GlobalConfig.loginEntity?.menuPermissionCode != null) {
      if((GlobalConfig.loginEntity?.menuPermissionCode ?? []).contains("12")) {
        bottomItems.add(BottomNavigationBarItem(
            icon: Image.asset('assets/images/tab_data_nor.webp',
                width: iconWidth, height: iconHeight),
            activeIcon: Image.asset('assets/images/tab_data_pre.webp',
                width: iconWidth, height: iconHeight),
            label: '统计'));
        _currentIndex = 1;
      }
    }
    bottomItems.add(BottomNavigationBarItem(
        icon: Image.asset('assets/images/tab_home_nor.png',
            width: iconWidth, height: iconHeight),
        activeIcon: Image.asset('assets/images/tab_home_pre.png',
            width: iconWidth, height: iconHeight),
        label: '首页'));
    bottomItems.add(BottomNavigationBarItem(
        icon: Image.asset('assets/images/tab_mine_nor.webp',
            width: iconWidth, height: iconHeight),
        activeIcon: Image.asset('assets/images/tab_mine_pre.png',
            width: iconWidth, height: iconHeight),
        label: '我的'));
    DioClient().checkVersion(context, false, autoUpload: true);
  }

  @override
  Widget build(BuildContext context) {
    return WillPopScope(
        child: Scaffold(
          body: _currBody,
          bottomNavigationBar: BottomNavigationBar(
              items: bottomItems,
              currentIndex: _currentIndex,
              onTap: (int index) {
                _changePage(index);
              },
              selectedItemColor: HDColors.green,
              unselectedItemColor: HDColors.grayHint,
              selectedFontSize: 12,
              unselectedFontSize: 12,
              type: BottomNavigationBarType.fixed),
        ),
        onWillPop: () async {
          if (_lastPressedAt == null ||
              DateTime.now().difference(_lastPressedAt!) >
                  const Duration(seconds: 2)) {
            //两次点击间隔超过1秒则重新计时
            _lastPressedAt = DateTime.now();
            BrnToast.show("再按一次退出", context);
            return false;
          }
          return true;
        });
  }

  //切换页面
  void _changePage(int index) {
    if (index != _currentIndex) {
      switch (bottomItems[index].label) {
        case "统计":
          _currBody = DataPage();
          break;
        case "首页":
          _currBody = ApplicationPage();
          break;
        case "我的":
          _currBody = MinePage();
          break;
      }
      setState(() {
        _currentIndex = index;
      });
    }
  }

  @override
  void dispose() {
    subscription?.cancel();
    WidgetsBinding.instance.removeObserver(this);
    super.dispose();
  }
}
